<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="电影ID" prop="movieId">
        <el-input
          v-model="queryParams.movieId"
          placeholder="请输入电影ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择类型" clearable>
          <el-option label="正片" value="episode" />
          <el-option label="预告片" value="trailer" />
          <el-option label="花絮" value="extra" />
          <el-option label="片段" value="clip" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option label="启用" value="0" />
          <el-option label="禁用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['movie:videoRelation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['movie:videoRelation:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoRelationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="movieVideoId" width="80" />
      <el-table-column label="电影ID" align="center" prop="movieId" width="100" />
      <el-table-column label="类型" align="center" prop="typeId" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.typeId === 'episode'" type="success">正片</el-tag>
          <el-tag v-else-if="scope.row.typeId === 'trailer'" type="warning">预告片</el-tag>
          <el-tag v-else-if="scope.row.typeId === 'extra'" type="info">花絮</el-tag>
          <el-tag v-else-if="scope.row.typeId === 'clip'">片段</el-tag>
          <el-tag v-else type="info">{{ scope.row.typeId }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="视频ID" align="center" prop="videoId" width="100" />
      <el-table-column label="封面" align="center" prop="cover" width="100">
        <template #default="scope">
          <image-preview v-if="scope.row.cover" :src="scope.row.cover" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" width="80" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.status === '0'" type="success">启用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['movie:videoRelation:edit']"
          >修改</el-button>
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['movie:videoRelation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="videoRelationRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="电影ID" prop="movieId">
          <el-input v-model="form.movieId" placeholder="请输入电影ID" />
        </el-form-item>
        <el-form-item label="类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择类型">
            <el-option label="正片" value="episode" />
            <el-option label="预告片" value="trailer" />
            <el-option label="花絮" value="extra" />
            <el-option label="片段" value="clip" />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="视频ID" prop="videoId">
          <el-input v-model="form.videoId" placeholder="请输入视频ID" />
        </el-form-item>
        <el-form-item label="封面">
          <image-upload v-model="form.cover" :limit="1" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="链接地址">
          <el-input v-model="form.link" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MovieVideoRelation">
import { listMovieVideo, getMovieVideo, delMovieVideo, addMovieVideo, updateMovieVideo } from "@/api/movie/videoRelation";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';

const { proxy } = getCurrentInstance();
const { parseTime } = proxy;

const videoRelationList = ref([]);
const open = ref(false);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    movieId: null,
    typeId: null,
    title: null,
    videoId: null,
    status: null
  },
  rules: {
    movieId: [
      { required: true, message: "电影ID不能为空", trigger: "blur" }
    ],
    typeId: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ],
    videoId: [
      { required: true, message: "视频ID不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询电影视频关联列表 */
function getList() {
  loading.value = true;
  listMovieVideo(queryParams.value).then(response => {
    videoRelationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    movieVideoId: null,
    movieId: null,
    typeId: null,
    title: null,
    videoId: null,
    cover: null,
    sort: 0,
    status: "0",
    link: null,
    remark: null
  };
  proxy.resetForm("videoRelationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.movieVideoId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加电影视频关联";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const movieVideoId = row.movieVideoId || ids.value;
  getMovieVideo(movieVideoId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改电影视频关联";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["videoRelationRef"].validate(valid => {
    if (valid) {
      if (form.value.movieVideoId != null) {
        updateMovieVideo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMovieVideo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const movieVideoIds = row.movieVideoId || ids.value;
  proxy.$modal.confirm('是否确认删除电影视频关联编号为"' + movieVideoIds + '"的数据项？').then(function() {
    return delMovieVideo(movieVideoIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
